
local save = {}
ac.game:event '游戏-初始化'(function(trg)
    trg:remove()
    for u in ac.selector('unit')
        : range(ac.point(0,0),20000)
        : can_god()
        : loop()
    do
        local id = u.owner.id
        local pt = u:get_point()
        local jd = u:get_facing()
        if save[id]==nil then
            save[id] = {}
        end
        save[id][#save[id] + 1] = {
            name = u.name,
            _id = FourCC(u._id),
            point = pt,
            angle = jd,
        }
        u:remove()
    end
end)


local rect1 = ac.rect.j_rect('第一幕')
local point_map = {
    ['出生点'] = 'point1',
    ['物品点'] = 'item_point',
    ['奇遇事件'] = 'shop_point1',
    ['神秘商店'] = 'shop_point2',
    ['矿工'] = 'kg_point1',
    ['矿场守护者'] = 'kg_point2',
}
local hide_shop = {
    ['迷宫商店'] = true,
    ['矿场商店'] = true,
    ['迷宫'] = true,
}
local enemy_unit = {
    ['塔1'] = true,
    ['塔2'] = true,
    --['迷宫'] = true,
    ['恶魔左使'] = true,
    ['恶魔右使'] = true,
    ['炽魔王'] = true,
    ['魔王'] = true,
}

ac.game:event '玩家-加载预设单位'(function(_,player)
    local id = player.id
    local enemy = ac.player[id + 8]
    for _,da in ipairs(save[id] or {}) do
        local pt = da.point
        local jd = da.angle
        local id = da._id
        local name = da.name
        local flag = rect1:has(da.point)
        local key1 = point_map[name]

        --记录点
        if key1 and flag then
            flag = false
            player[key1] = pt
        end

        if flag then
            local u = player:create_unit(id,pt,jd)
            u.owner_player = player
            player:set_data(name,u)
            if name=='基地' then
                ac.base_point = pt
                ac.base_unit = u
                u:add_skill('基地设置')
            end
            if hide_shop[name] then
                u:add_restriction '隐藏'
                shop_add_panel(u)
                player:event '玩家-显示商店'(function(trg,player,name)
                    if name==u.name then
                        trg:remove()
                        u:remove_restriction '隐藏'
                        local effect = u:add_effect([[GN_gthts.mdl]])
                        if ac.enemy.hard<3 then
                            player:set_camera(point,0.5)
                        end
                        player:event '玩家-选择单位'(function(trg,_,select_unit)
                            if select_unit==u then
                                trg:remove()
                                effect:remove()
                            end
                        end)
                    end
                end)
            end
            
            --怪物
            if enemy_unit[name] then
                u.owner_player = player
                u:set_owner(enemy)
                u:add_type('建筑')
                u:add_buff '动态测试数据'{}
                if name=='塔1' then
                    u:set('攻击间隔',1.5)
                end
            end
        end
    end
end)


ac.game:event '游戏-设置难度结束'(function(_,hard)
    ac.init_game = true
    ac.player[5]:notify('玩家-加载预设单位',ac.player[5])
    ac.each_player(function(player)
        ac.player[player.id + 8]:set_name(('%s的敌人'):format(player.name))
        player.atk1 = ac.rect.j_rect(('SB%s_1'):format(player.id))
        player.atk2 = ac.rect.j_rect(('SB%s_2'):format(player.id))
        player.zb_rect = ac.rect.j_rect(('zb_%s'):format(player.id))
        player:notify('玩家-加载预设单位',player)
        player:notify('玩家-注册数据',player)
    end)
    ac.game:notify('游戏-注册数据完成')
end)

